package com.bicon.botu.finance.wallet.component;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bicon.botu.finance.wallet.common.FinanceConstants;
import com.bicon.botu.finance.wallet.common.WalletConstants;
import com.bicon.botu.finance.wallet.entity.WalletDetail;
import com.bicon.botu.finance.wallet.service.WalletDetailService;
import com.bicon.botu.core.base.common.utils.BeanMapper;
import com.bicon.botu.core.base.common.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 钱包定时任务
 * @author 谈东魁
 * @date 2017/9/10 0010 下午 16:23
 */
@Slf4j
@Component
public class WalletTimedTask {

    @Autowired
    WalletDetailService walletDetailService;

    /**
     * 转账退款定时任务
     * 每天0点执行
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void transferAccountsRefund() {
        log.info("转账订单退款任务【开始】...");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -1);
        WalletDetail walletDetail = new WalletDetail();
        //查询所有待退款订单
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.eq("subjects_child", FinanceConstants.WALLET_CHILD_SUBJECTS_TRANSFER);
        entityWrapper.eq("state", WalletConstants.WALLET_STATE_RECEIVABLE);
        entityWrapper.lt("create_date", DateUtil.DateToString(calendar.getTime(), "yyyy-MM-dd HH:mm:ss"));
        List<WalletDetail> list = walletDetail.selectList(entityWrapper);
        //每个转账订单进行退款，一个订单一个事物
        for(WalletDetail wd : list){
            EntityWrapper e = new EntityWrapper();
            BeanMapper.copy(entityWrapper, e);
            e.eq("id", wd.getId());
            if(walletDetailService.transferAccountsRefundTask(e, wd)){
                log.info("["+wd.getId()+"]["+wd.getOrderNo()+"]转账订单退款成功 [success]");
            }else{
                log.info("["+wd.getId()+"]["+wd.getOrderNo()+"]转账订单退款失败 [fail]");
            }
        }
        log.info("转账订单退款任务【结束】");
    }

    /**
     * 转账、充值超过1小时交易中的订单设为取消状态定时任务
     * 每小时执行一次
     */
    @Scheduled(cron = "0 0 * * * ?")
    public void tradingToCancel() {
        log.info("转账|充值超时订单取消任务【开始】...");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, -1);
        WalletDetail walletDetail = new WalletDetail();
        //查询所有转账、充值 | 交易中 | 一小时前的订单
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.andNew("subjects_child = " + FinanceConstants.WALLET_CHILD_SUBJECTS_TRANSFER + " or subjects_child = " + FinanceConstants.WALLET_CHILD_SUBJECTS_RECAHRGE);
        entityWrapper.andNew("state = " + WalletConstants.WALLET_STATE_ING);
        entityWrapper.lt("create_date", DateUtil.DateToString(calendar.getTime(), "yyyy-MM-dd HH:mm:ss"));
        List<WalletDetail> list = walletDetail.selectList(entityWrapper);
        for(WalletDetail wd : list){
            WalletDetail w = new WalletDetail();
            w.setState(WalletConstants.WALLET_STATE_CANCEL);
            w.preUpdate();
            w.update(new EntityWrapper().eq("order_no", wd.getOrderNo()));
            log.info("["+wd.getId()+"]["+wd.getOrderNo()+"]转账|充值超时订单已设为取消 [success]");
        }
        log.info("转账|充值超时订单取消任务【结束】");
    }
}
